package com.onstar.cn.weight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;

/**
 * 
 * @author jun chen
 * @ClassName: CustomBtnDrawableLeftCenter 
 * @date 2015年12月3日 下午7:25:05 
 * @Description: 自定义的Button 使drawableLeft 居中显示
 */
public class CustomBtnDrawableRightCenter extends Button{

	public CustomBtnDrawableRightCenter(Context context) {
		super(context);
	}
	
	public CustomBtnDrawableRightCenter(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public CustomBtnDrawableRightCenter(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	
	@Override
	protected void onDraw(Canvas canvas) {
		
		 Drawable[] drawables = getCompoundDrawables();  
	        if (drawables != null) {  
	            Drawable drawableRight = drawables[2];  
	                if (drawableRight != null) {  
	                float textWidth = getPaint().measureText(getText().toString());  
	                int drawablePadding = getCompoundDrawablePadding();  
	                int drawableWidth = 0;  
	                drawableWidth = drawableRight.getIntrinsicWidth();  
	                float bodyWidth = textWidth + drawableWidth + drawablePadding;  
	                setPadding(0, 0, (int)(getWidth() - bodyWidth), 0);  
	                canvas.translate((getWidth() - bodyWidth) / 2, 0);  
	            }  
	        }  
		super.onDraw(canvas);
	}
}